package com.website.system.utils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class HandlingTimeAspect {
   private Logger logger = LoggerFactory.getLogger(HandlingTimeAspect.class);
    @Pointcut("@annotation(com.website.system.utils.HandlingTime)")
    public void handlingTimePointcut() {}

    @Around("handlingTimePointcut()")
    public Object handlingTimeAround(ProceedingJoinPoint joinPoint){
        try {
            long startTime = System.currentTimeMillis();
            Object proceed = joinPoint.proceed();
            MethodSignature signature= (MethodSignature) joinPoint.getSignature();
            String methodName=signature.getName();
            System.out.println("----执行"+methodName+"方法，用时：" + (System.currentTimeMillis() - startTime)+"ms ----");
            logger.info(("----执行"+methodName+"方法，用时：" + (System.currentTimeMillis() - startTime)+"ms ----"));
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
